using System;
using System.Collections.Generic;
using System.Linq;
using Cooking.Core.Model;
namespace Cooking.Core
{
    public class Reorderer<T>  where T:IOrderable
    {
        private readonly IList<T> sourceData;

        public Reorderer(IList<T> sourceData)
        {
            this.sourceData = sourceData;
        }

        private void SwitchPosition(T sourceItem, Func<T, bool> predicate)
        {
            int currentPosition = sourceItem.SequenceNumber;
            var destinationItem = sourceData.FirstOrDefault(predicate);

            if (!Equals(destinationItem, null))
            {
                sourceItem.SequenceNumber = destinationItem.SequenceNumber;
                destinationItem.SequenceNumber = currentPosition;
            }
        }

        public void MoveUp(T sourceItem)
        {
            SwitchPosition(sourceItem, x => x.SequenceNumber == sourceItem.SequenceNumber - 1);
        }

        public void MoveDown(T sourceItem)
        {
            SwitchPosition(sourceItem, x => x.SequenceNumber == sourceItem.SequenceNumber + 1);
        }

        
    }
}